Ext.define('Service.comUser.controller.ComUserController', {
	extend: 'WebOs.ex.DefaultController',
	
	views : [
		'Service.comUser.view.InitContainer',
		'Service.comUser.view.ComUserGird',
		'Service.comUser.view.ComUserAddWindow',
		'Service.comUser.view.ComUserChgWindow',
		'Service.comUser.view.ComUserDetailWindow'
	],
	
	stores : [
			'Service.comUser.store.HtComUserStore'
			
	],
	
	models : [
			'Service.comUser.model.HtComUserModel'
			
	],
	
	refs : [
		//getter setter
	],
	
	condition : {
		
	},
	
	
	init: function(h,config) {
		var me=this;
		Ext.apply(me,config);
		this.control({
			'button[action=addHtComUser]' : {
				click : me.onAddComUserBtnClickFire
			},
			'button[action=chgHtComUser]' : {
				click : me.onChgComUserBtnClickFire
			},
			'button[action=delHtComUsers]' : {
				click : me.onDelComUserBtnClickFire
			},
			'button[id=Service_mainPage_view_ComUserSaveBtn]' : {
				click : me.onComUserSaveBtnClickFire
			},
			'button[id=Service_mainPage_view_ComUserCancelBtn]' : {
				click : me.onComUserCancelBtnClickFire
			},
			'button[action=chgHtComUser]' : {
				click : me.onChgComUserClickFire
			},
			'gridpanel[id=Service_comUser_view_ComUserGird]' : {
				afterrender : me.afterUserGridRenderFire
			},
			'button[id=Service_mainPage_view_ComUserChgSaveBtn]' : {
				click : me.onComUserChgSaveBtnClickFire
			},
			'button[id=Service_mainPage_view_ComUserChgCancelBtn]' : {
				click : me.onComUserChgCancelBtnClickFire
			},
			'button[action=delHtComUsers]' : {
				click : me.onDelComUsersBtnClickFire
			},
			'combo[identifier=Service_comUser_view_ComCombo]' : {
				select : me.onComComboSelectFire
			},
			'window[id=Service_comUser_view_ComUserDetailWindow]' : {
				afterrender : me.onDetailWindowAfterRenderFire
			}
		});
	},
	
	onDetailWindowAfterRenderFire : function(cmp){
		var basicForm = cmp.down('form[info=basic]').getForm();
		var otherForm = cmp.down('form[info=other]').getForm();
		if(cmp.record){
			console.log(cmp.record.data);
			basicForm.loadRecord(cmp.record);
			otherForm.loadRecord(cmp.record);
			if(cmp.record.get('img')){
				var imgUrl = 'resources/imgs/user/' + cmp.record.get('img');
				document.getElementById('comUser_preview').src=imgUrl;
			}
		}
	},
	
	onComUserChgSaveBtnClickFire : function(btn){
		var fileForm = btn.up('window').down('form[uploadType=img]').getForm();
		var form = btn.up('window').down('form[uploadType=normal]').getForm();
		if (btn.up('window').down('fileuploadfield').getValue()) {
			if (fileForm.isValid() && form.isValid()) {
				fileForm.submit({
					clientValidation : true,
					url : "imgUpload?type=user",
					waitMsg : '图片上传中',
					success : function(cm, resp) {
						var result = Ext.decode(resp.response.responseText);
						if (result['success'] == "true") {
							var param = form.getValues();
							Ext.apply(param, {
								img : result['result'],
								module : 'Service_comUser',
								action : 'chgHtComUser',

							});
							Ext.Ajax.request({
								url : Constants.REQUEST_URL,
								method : 'POST',
								params : param,
								success : function(fp, o) {
									Ext.Msg.alert('成功', '修改成功');
									Ext.getCmp('Service_comUser_view_ComUserGird').getStore().loadPage(1);
									btn.up('window').destroy();
								}
							});
						} else {
							alert("错误的文件类型,只支持png,jpg,jpeg,gif格式");
						}
					}
				});
			}
		}else{
			var param = form.getValues();
			Ext.apply(param, {
				module : 'Service_comUser',
				action : 'chgHtComUser',

			});
			Ext.Ajax.request({
				url : Constants.REQUEST_URL,
				method : 'POST',
				params : param,
				success : function(fp, o) {
					Ext.Msg.alert('成功', '修改成功');
					Ext.getCmp('Service_comUser_view_ComUserGird').getStore().loadPage(1);
					btn.up('window').destroy();
				}
			});
		}
	},
	
	onComUserChgCancelBtnClickFire : function(btn){
		btn.up('window').destroy();
	},
	onComComboSelectFire : function(cmp){
		var comId = cmp.getValue();
		var condition = {areaId : comId};
		var store  = Ext.getCmp('Service_comUser_view_ComUserGird').getStore();
		store.resetRequestParams();
		store.addRequestParams(condition);
		store.loadPage(1);
	},
	
	afterUserGridRenderFire : function(cmp){
		var me = this;
		var store = cmp.getStore();
		store.addRequestParams(me.condition);
		if(me.condition.areaId){
			var combo = cmp.up('container').down('combo[identifier=Service_comUser_view_ComCombo]');
			combo.select(me.condition.areaId);
		}
		store.load();
		me.condition = {};
	},
	
	onDelComUsersBtnClickFire : function(cmp){
		var me = this;
		var grid = Ext.getCmp('Service_comUser_view_ComUserGird');
		var sel = grid.getSelectionModel().getSelection();
		if(sel.length < 1){
			Ext.Msg.alert('提示','请至少选择一名住户进行删除');
		}else{
			var ids  = [];
			for(var p in sel){
				ids.push(sel[p].get('id'));
			}
			Ext.MessageBox.confirm('操作确认', '确定要删除这'+ids.length+'项吗?', function(btn) {
				if (btn == 'yes') {
					me.request({
						params : {
							ids : Ext.encode(ids),
							module : 'Service_comUser',
							action : 'delHtComUsers'
						},
						onSuccess : function(){
							grid.getStore().loadPage(1);
						}
					});
				}
			}, this);
			
		}
	},
	
	onChgComUserClickFire : function(cmp){
		var grid = Ext.getCmp('Service_comUser_view_ComUserGird');
		var sel = grid.getSelectionModel().getSelection();
		if(sel.length != 1){
			Ext.Msg.alert('提示','请选择一个住户');
		}else{
			var win = Ext.create('Service.comUser.view.ComUserChgWindow');
			win.down('form[uploadType=normal]').loadRecord(sel[0]);
			win.show();
		}
	},
	
	
	onComUserCancelBtnClickFire : function(btn){
		btn.up('window').destroy();
	},
	onComUserSaveBtnClickFire : function(btn){
		var fileForm = btn.up('window').down('form[uploadType=img]').getForm();
		var form = btn.up('window').down('form[uploadType=normal]').getForm();
		if(fileForm.isValid() && form.isValid()){
			fileForm.submit({
				clientValidation : true,
				url : "imgUpload?type=user",
				waitMsg : '图片上传中',
				success : function(cm, resp){
					var result = Ext.decode(resp.response.responseText);
					if (result['success'] == "true") {
						var param = form.getValues();
						Ext.apply(param, {
							img : result['result'],
							module : 'Service_comUser',
							action : 'addHtComUser',

						});
						Ext.Ajax.request({
							url : Constants.REQUEST_URL,
							method : 'POST',
							params : param,
							success : function(fp, o) {
								Ext.Msg.alert('成功', '保存成功');
								Ext.getCmp('Service_comUser_view_ComUserGird').getStore().loadPage(1);
								btn.up('window').destroy();
							}
						});
					} else {
						alert("错误的文件类型,只支持png,jpg,jpeg,gif格式");
					}
					
				}
			});
		}
		
	},
	
	onAddComUserBtnClickFire : function(btn){
		var comcombo = btn.up('gridpanel').down('comcombo');
		var comId = comcombo.getValue();
		if(comId){
			Ext.create('Service.comUser.view.ComUserAddWindow',{areaId : comId,comName : comcombo.getRawValue()}).show();
		}else{
			Ext.Msg.alert('提示','请选择一个小区');
		}
		
	},
	onChgComUserBtnClickFire : function(btn){
		
	},
	onDelComUserBtnClickFire : function(btn){
		
	}
	
});
